package com.nwpu.coursesupport.service.imple;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.Status;
import com.nwpu.coursesupport.enumeration.TaskType;
import com.nwpu.coursesupport.pagination.PageBounds;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.view.SCCView;
import com.nwpu.coursesupport.view.TaskListStuView;
import com.nwpu.coursesupport.view.criteria.TaskListStuViewSearchCriteria;
import com.nwpu.coursesupport.view.mapper.SCCViewMapper;
import com.nwpu.coursesupport.view.mapper.TaskListStuViewMapper;

@Service
public class TaskListStuViewService extends AbstractService{
	
	@Autowired
	private TaskListStuViewMapper taskListStuViewMapper;
	@Autowired
	private SCCViewMapper sCCViewMapper;
	
    protected static Logger logger = LoggerFactory.getLogger(TaskListStuViewService.class);
	
	public JSONResult taskList(TaskListStuViewSearchCriteria sc){
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(sc==null||sc.getClassId()<=0){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		
		SCCView view= this.sCCViewMapper.findById(sc.getClassId());
         
        if(view==null){
        	logger.error("该班级不存在");
        	return rst.fail("该班级不存在");
        }
		
		if (sc.getPageNo() <= 0) {
			sc.setPageNo(1);
		}
		sc.setIsShow(Status.YES);
				
		int pageSize = 15;
		int totalCount = -1;
		int pageCount = -1;
		
		List<TaskListStuView> list = null;
		List<TaskListStuView> list1 = null;

		if(sc.getType()==null){
			list=new ArrayList<TaskListStuView>();
			sc.setType(null);
			sc.setType(TaskType.TASKMAIN);
			list.addAll(getTaskMainView(sc));
			list.addAll(getTaskInLessonView(sc));
			
			/*need improve-start*/
			totalCount = list.size();
			if(totalCount < 0){
				logger.error("获取任务失败");
				return rst.fail("获取数据失败");
			}
			pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount /pageSize;
			if (sc.getPageNo() > pageCount) {
				sc.setPageNo(pageCount);
			}
			list1 = getPage(sc.getPageNo(),list,pageSize,totalCount,pageCount);
			/*need improve-end*/
			
			sorted(list1);
		}else if(sc.getType()==TaskType.ASKINLESSON){
			/*getTaskList-askInLesson*/					
			list=getTaskInLessonView(sc);
			
			/*need improve-start*/
			totalCount = list.size();
			if(totalCount < 0){
				logger.error("获取任务失败");
				return rst.fail("获取数据失败");
			}
			pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount /pageSize;
			
			if (sc.getPageNo() > pageCount) {
				sc.setPageNo(pageCount);
			}
			list1 = getPage(sc.getPageNo(),list,pageSize,totalCount,pageCount);
			/*need improve-end*/
			
		}else{
			/*getTaskList-otherTask*/
			list=getTaskOtherView(sc);
			
			/*need improve-start*/
			totalCount = list.size();
			if(totalCount < 0){
				logger.error("获取任务失败");
				return rst.fail("获取数据失败");
			}
			pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount /pageSize;
			
			if (sc.getPageNo() > pageCount) {
				sc.setPageNo(pageCount);
			}
			list1 = getPage(sc.getPageNo(),list,pageSize,totalCount,pageCount);
			/*need improve-end*/

		}
		
		/*if need exist*/
		if(pageCount < 0){
			logger.error("获取数据失败");
			return rst.fail("获取数据失败");
		}
		rst.addData("list", list1);
		rst.addData("type", TaskType.TASKMAIN.name());
		rst.addData("classView", view);
		rst.addData("pageNo", sc.getPageNo());
		rst.addData("allpages",pageCount);
		rst.success();
		return rst;
	}
	
	private List<TaskListStuView> getPage(int pageNo, List<TaskListStuView> list, int pageSize, int totalCount, int pageCount){
		System.out.println("pageNo:"+pageNo+"pageSize:"+pageSize+"totalCount:"+totalCount+"pageCount:"+pageCount);
		if(pageCount <= 0 ){
			return list;
		}

		if(pageNo == pageCount){
			if(pageNo == 1){
				return list;
			}else{
				return list.subList(pageSize*(pageNo - 1), totalCount);
			}
			
		}else {
			return list.subList(pageSize*(pageNo - 1), pageSize*pageNo);
		}
	}
	
	private List<TaskListStuView> getTaskMainView(TaskListStuViewSearchCriteria sc){
		List<TaskListStuView> list=this.taskListStuViewMapper.getTaskMainView(sc);
		return list;
	}
	
	private List<TaskListStuView> getTaskOtherView(TaskListStuViewSearchCriteria sc){
		List<TaskListStuView> list=this.taskListStuViewMapper.getTaskByCourseId(sc);
		return list;
	}
	private List<TaskListStuView> getTaskInLessonView(TaskListStuViewSearchCriteria sc){
		sc.setType(TaskType.ASKINLESSON);
		List<TaskListStuView> list=this.taskListStuViewMapper.getTaskInLessonView(sc);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static void sorted(List<TaskListStuView> list){
		if(list==null){
			return ;
		}
		Collections.sort(list, new Comparator(){
			@Override
			public int compare(Object o1, Object o2) {
				TaskListStuView p1 = (TaskListStuView) o1;   
				TaskListStuView p2 = (TaskListStuView) o2; 
				return p2.getAssignDate().compareTo(p1.getAssignDate());
			}
			
		});
	}
	
	public JSONResult taskPersonalList(TaskListStuViewSearchCriteria sc){
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(sc==null||sc.getClassId()<=0){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		
		SCCView view= this.sCCViewMapper.findById(sc.getClassId());
        
        if(view==null){
        	logger.error("该班级不存在");
        	return rst.fail("该班级不存在");
        }
        sc.setCourseId(view.getCourseId());
        
		sc.setType(TaskType.PERSONALTASK);
		int id=(int) getSession().getAttribute("id");
		sc.setAssignedId(id);
		
		if (sc.getPageNo() <= 0) {
			sc.setPageNo(1);
		}

		int pageSize = 20;
		int totalCount = this.taskListStuViewMapper.countTaskPersonByCourseId(sc);
		int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
		if (sc.getPageNo() > pageCount) {
			sc.setPageNo(pageCount);
		}
		PageBounds page = new PageBounds();
		page.setNumber(sc.getPageNo());
		page.setSize(pageSize);
		
		List<TaskListStuView> list=this.taskListStuViewMapper.getTaskPersonByCourseId(sc, page);
		rst.addData("list", list);
		rst.addData("type",TaskType.PERSONALTASK.name());
		rst.addData("classView", view);
		rst.addData("pageNo", sc.getPageNo());
		rst.addData("allpages",pageCount);
		rst.success();
		return rst;
	}
}
